<!--智慧消防V1.0-edit.vue页面-->
<template>
    <div class="app-container">
        <el-row :gutter="10">
            <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
                <div style="text-align: center;margin-bottom: 30px">
                    位置信息{{createStatus?'新增':'更新'}}
                </div>
                <el-form :model="placeForm" ref="placeForm" :rules="placeFormRules" label-width="100px">
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="位置名称" prop="name">
                            <el-input type="text" v-model="placeForm.name" placeholder="请输入位置名称" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="位置编码"  prop="code">
                            <el-input v-model="placeForm.code" placeholder="请输入位置编码" clearable></el-input>
                        </el-form-item>
                    </el-col>
<!--                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">-->
<!--                        <el-form-item label="道路信息 " prop="road">-->
<!--                            <el-input v-model="placeForm.road" placeholder="请输入位置道路信息" clearable></el-input>-->
<!--                        </el-form-item>-->
<!--                    </el-col>-->
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属建筑" prop="build">
                            <el-select v-model="placeForm.build" placeholder="请选择所属建筑" style="width: 100%">
                                <el-option v-for="(item,index) in buildingList" :key="index"  :value="item.ucId" :label="item.name" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="所属企业" prop="enterpriseId" v-if="enterPriseHidden">
                            <el-select v-model="placeForm.enterpriseId" placeholder="请选择所属建筑" style="width: 100%">
                                <el-option v-for="(item,index) in enterpriseList" :key="index"  :value="item.ucId" :label="item.enterpriseName" ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="楼层" prop="floor">
                            <el-input type="text" v-model="placeForm.floor" placeholder="请输入楼层信息" clearable></el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :xs="24" :sm="24" :md="12" :lg="12" :xl="8">
                        <el-form-item label="详细地址" prop="location">
                            <el-input type="text" v-model="placeForm.location" placeholder="请输入详细地址" clearable></el-input>
                        </el-form-item>
                    </el-col>
                </el-form>
                <div style="text-align: center" class="dialog-footer">
                    <el-button @click="cancelPlace">取 消</el-button>
                    <el-button :loading="addLoading" v-if="createStatus" type="primary" @click="createPlace">创 建</el-button>
                    <el-button :loading="addLoading" v-else type="primary" @click="updatePlace">修 改</el-button>
                </div>
            </el-col>
        </el-row>
    </div>
</template>

<script>
import {
    addInPlaceInfo, getInEnterpriseList,
    getInPlaceInfo, getInUserVo,
    updateInPlaceInfo
} from "@/api/enterprise";
import {getBuildingList} from "@/api/building";


export default {
    name: "edit",
    data(){
        return{
            placeForm:{},
            placeFormRules:{
                name: [{required: true, message: '位置名称不能为空', trigger: 'blur'}],
                build: [{required: true, message: '所属建筑不能为空', trigger: 'blur'}],
                enterpriseId: [{required: true, message: '所属企业不能为空', trigger: 'blur'}],
            },
            addLoading:false,
            buildingList:[],
            enterpriseList:[],
        }
    },
    computed:{
        createStatus(){
            return this.$route.params.ucId =='0'
        },
      enterPriseHidden(){
        let roleCode = this.$store.getters.roleCode;
        return roleCode=="ADMIN"||roleCode=="XT-ADMIN"||roleCode=="ZD-ADMIN"||roleCode=="DD-ADMIN"||roleCode=="JW-ADMIN"
      },
    },
    created() {
        this.getBuildingList()
       this.getInEnterpriseList()
        if (!this.createStatus){
            this.getPlaceInfo()
        }
    },
    methods:{
        cancelPlace(){
          window.history.back()
        },
        createPlace(){
            this.addLoading = true
            this.$refs['placeForm'].validate(valid => {
                if (valid){
                    addInPlaceInfo(this.placeForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "添加位置信息成功"
                        })
                        this.cancelPlace()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        updatePlace(){
            this.addLoading = true
            this.$refs['placeForm'].validate(valid => {
                if (valid){
                    updateInPlaceInfo(this.placeForm).then(res=>{
                        this.addLoading = false
                        this.$notify.success({
                            title: '成功',
                            message: "更新位置信息成功"
                        })
                        this.cancelPlace()
                    }).catch(res=>{
                        this.addLoading = false
                        this.$notify.error({
                            title: '失败',
                            message: res.msg
                        })
                    })
                }else {
                    this.addLoading = false
                    console.log("表单验证不通过")
                }
            })
        },
        getPlaceInfo(){
            getInPlaceInfo({ucId:this.$route.params.ucId}).then(res=>{
                this.placeForm = res.data
            })
        },
        getBuildingList(){
            getBuildingList({deleted:false}).then(res=>{
                this.buildingList = res.data
            }).catch(error=>{
                alert("获取建筑列表失败")
            })
        },
        getInEnterpriseList(){
            getInEnterpriseList({deleted:false}).then(res=>{
                this.enterpriseList = res.data
            }).catch(error=>{

            })
        }



    }
}
</script>

<style scoped>

</style>